<?php

namespace App\Console\Commands\Once;

use App\Kuafu\Log\Model\UserLoginLogModel;
use App\Kuafu\User\Model\UserFirstLoginModel;
use Illuminate\Console\Command;

class InitUserFirstLoginCommand extends Command
{
    protected $signature = 'once:init-user-first-login';

    protected $description = '初始化用户首次登录记录';


    public function handle()
    {
        UserFirstLoginModel::truncate();

        UserLoginLogModel::query()
                         ->where('type', 4)
                         ->groupBy('userId')
                         ->select('userId', 'createdAt')
                         ->orderBy('createdAt')
                         ->get()
                         ->map(function ($item) {
                             return [
                                 'userId'    => $item->userId,
                                 'type'      => 4,
                                 'createdAt' => $item->createdAt->timestamp,
                             ];
                         })
                         ->chunk(1000)
                         ->each(function ($item) {
//                             dump($item->values());

                             UserFirstLoginModel::query()->insert($item->values()->toArray());
                         });
    }
}
